package com.borball.store.comp;
import java.awt.BorderLayout;
import java.sql.Timestamp;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableRowSorter;

import com.borball.store.dao.MStoreLogDAO;
import com.borball.store.domain.MStoreLog;

import craky.component.CommonTableModel;
import craky.componentc.JCScrollTable;
import craky.componentc.JCTable;
import craky.componentc.JCTableHeader;
import craky.keeper.EmptyComponent;
import craky.keeper.KeeperPane;

public class MStoreLogPane extends KeeperPane {

	private static final long serialVersionUID = 2583431180094797757L;

	protected JCTable table;
	protected List<MStoreLog> data;
	protected CommonTableModel<MStoreLog> model;
	private static final String[] COLUMNS_NAME = { "id", "元件类型", "变更类型", "变更数量", "操作时间", "备注", "经手人", "操作员" };
	protected JComponent bottom, center;
	private MStoreLogDAO dao;

	public MStoreLogPane() {
		dao = new MStoreLogDAO();
		loadData();
		initUI();
	}


	private void loadData() {
		data = dao.findAll();
	}

	private void initUI() {
		EmptyComponent centerParent = new EmptyComponent();
		setLayout(new BorderLayout());
		centerParent.setLayout(new BorderLayout(0, 3));
		createCenter();

		this.add(centerParent, BorderLayout.CENTER);

		if (center != null) {
			centerParent.add(center, BorderLayout.CENTER);
		}

	}


	@Override
	public String getType() {
		return "元件库存变更";
	}

	protected JComponent createCenter() {
		final Class<?>[] columnsClass = { Integer.class, String.class, String.class, Integer.class, Timestamp.class,
				String.class, String.class, String.class };
		final String[] getMethodsName = { "getId", "getMaterial", "getAction", "getAmount", "getCreated", "getMemo",
				"getUser", "getOperator" };
		final String[] setMethodsName = { "setId", "setMaterial", "setAction", "setAmount", "setCreated", "setMemo",
				"setUser", "setOperator" };
		final int[] columnsPreferredWidth = { 10, 20, 16, 8, 60, 220, 16, 16 };
		EmptyComponent ec = new EmptyComponent();
		JCScrollTable scTable = new JCScrollTable();
		table = scTable.getTable();
		model = new CommonTableModel<MStoreLog>(table, COLUMNS_NAME, columnsClass, getMethodsName, setMethodsName,
				data);
		TableRowSorter<CommonTableModel<MStoreLog>> sorter = new TableRowSorter<CommonTableModel<MStoreLog>>(
				model);

		scTable.setAlpha(0.0f);
		scTable.setColumnControlEnabled(false);

		ec.setLayout(new BorderLayout());
		ec.add(scTable, BorderLayout.CENTER);
		table.setModel(model);
		table.setDragEnabled(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowSorter(sorter);
		table.setRendererOpaque(false);
		((JCTableHeader) table.getTableHeader()).setShowPopupMenu(false);

		for (int i = 0; i < columnsPreferredWidth.length; i++) {
			table.getColumnModel().getColumn(i).setPreferredWidth(columnsPreferredWidth[i]);
		}

		return center = ec;
	}


	@Override
	public void refresh() {
		loadData();
		model.delAllRow();
		model.setDataList(data);
		model.getTable().repaint();
		model.refreshRowOnView(0);
	}

}
